#include "game_stage.h"

game_stage * game_stage_instance = NULL;

game_stage * game_stage::self(){
	if (!game_stage_instance)
		game_stage_instance = new game_stage;
	return game_stage_instance;
}

game_stage::game_stage (){
	if (load_settings()!=0){
		/*TODO nacitaj default*/
		FOR(i,GSI_NUM)
			_ie[i]=0;
		FOR(i,GSF_NUM)
			_fe[i]=0;
		
		_ie[GSI_CARTS_NUM]=1;
		_fe[GSF_PLAYER_SPEED]=10;
		_fe[GSF_PLAYER_MAXSPEED]=20;
		_fe[GSF_PLAYER_MAXSIDESPEED]=10;
		_fe[GSF_PLAYER_TURN]=4;
		_fe[GSF_PLAYER_MAXTURN]=1;
		_fe[GSF_PLAYER_FRICTION_FRONT]=3;
		_fe[GSF_PLAYER_FRICTION_SIDE]=30;
		_fe[GSF_PLAYER_FRICTION_ROTATE]=0.4;

		
//		save_settings();
	}
}

int game_stage::load_settings(){
	FILE *config = fopen("./config_game_stage","r");
	if (config==NULL)
		return -1;
	FOR(i,GSI_NUM)
		if (fscanf(config,"%d",&_ie[i])==EOF)
			return -1;
	FOR(i,GSF_NUM)
		if (fscanf(config,"%f",&_fe[i])==EOF)
			return -1;
	return 0;
}

int game_stage::save_settings(){
	FILE *config = fopen("./config_game_stage","w");
	if (config==NULL)
		return -1;
	FOR(i,GSI_NUM)
		fprintf(config,"%d\n",_ie[i]);
	fprintf(config,"\n\n");
	FOR(i,GSF_NUM)
		fprintf(config,"%f\n",_fe[i]);
	return 0;
}
